package cn.tedu.boot34.controller;



import cn.tedu.boot34.mapper.UserMapper;
import cn.tedu.boot34.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;


    @RequestMapping("/reg")
    public int reg(@RequestBody User user) {
        User u = mapper.selectByUsername(user.getUsername());
        if (u != null) {
            return 2; //代表用户名已存在
        }
        //执行注册
        mapper.insert(user);
        return 1; //代表注册成
    }
    @RequestMapping("/login")
    public int login(@RequestBody User user){
        //拿用户输入的用户名查询数据库里面的用户信息
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null){//代表查询到了用户信息
            //判断用户输入的密码和查询到的密码是否一致
            if (user.getPassword().equals(u.getPassword())){
                return 1;//登录成功!
            }
            return 3;  //密码错误!
        }
        return 2;//代表用户名不存在
    }
}


